//
//  RHVideoAuditCell.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/7/12.
//

#import "RHVideoAuditCell.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import <SDWebImage/UIView+WebCache.h>
@interface RHVideoAuditCell()

//审核状态
@property (nonatomic, strong) QMUIButton *auditStatusBtn;

//
@property (nonatomic, strong) QMUILabel *plantNameLab;

@property (nonatomic, strong) QMUIButton *deleteBtn;

@property (nonatomic, strong) UIImageView *videoCoverView;//视频图层

@property (nonatomic, strong) QMUILabel *remarkLab;

@property (nonatomic, strong) UIVisualEffectView *effectView;

@property (nonatomic, strong) UIView *videoView;

@property (nonatomic, strong) MASConstraint *videoTopContraint;
@end
@implementation RHVideoAuditCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self setupUI];
    }
    return self;
}


-(void)setVideoModel:(RHVideoModel *)videoModel
{
    _videoModel = videoModel;
    self.titleLab.text = videoModel.title;
//    self.titleLab.text = @"asdarwqr123qrw1qw54r64qw65r4qw56r46q5w4r65qw4r65qw4r65qw4r65qw4r65qw4r65qw4r65qw4r65qw4r65qw4r654qw65r4qw56r465qw4r65qw4r";
    self.plantNameLab.text = ValidNullDataToString(videoModel.plantName, @"--");
    [self.videoCoverView sd_setImageWithURL:[NSURL URLWithString:videoModel.cover] placeholderImage:[UIImage imageNamed:@"video_loading"]];
    self.remarkLab.hidden = YES;
    self.effectView.hidden = YES;
    if(videoModel.checkStatus == 1){
        [self.auditStatusBtn setTitleColor:kCOLOR_HEX_A(@"#3F4443", 0.4) forState:UIControlStateNormal];
        [self.auditStatusBtn setTitle:@"已分享".rh_localized forState:UIControlStateNormal];
        self.auditStatusBtn.layer.cornerRadius = 8;
        self.auditStatusBtn.layer.borderWidth = 1;
        self.auditStatusBtn.layer.borderColor = kCOLOR_HEX(@"#EBEDF0").CGColor;
        [self.auditStatusBtn setGradientBackgroundWithColors:@[kCOLOR_HEX(@"#FFFFFF"),kCOLOR_HEX(@"#FFFFFF")] orientation:GradientOrientationLeftToRight];
        
    }else if (videoModel.checkStatus == 2 || videoModel.checkStatus == 3){
        [self.auditStatusBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        [self.auditStatusBtn setTitle:videoModel.checkStatus == 2?@"未通过".rh_localized:@"已下架".rh_localized forState:UIControlStateNormal];
        self.auditStatusBtn.layer.cornerRadius = 8;
        self.auditStatusBtn.layer.borderWidth = 0;
        [self.auditStatusBtn setGradientBackgroundWithColors:@[kCOLOR_HEX(@"#FB6868"),kCOLOR_HEX(@"#FB6868")] orientation:GradientOrientationLeftToRight];
        self.effectView.hidden = NO;
        self.remarkLab.hidden = NO;
        self.remarkLab.text = videoModel.checkStatus == 2?ValidNullData(videoModel.remark):@"";
    }else if (videoModel.checkStatus == 0){
        [self.auditStatusBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        [self.auditStatusBtn setTitle:@"待审核".rh_localized forState:UIControlStateNormal];
        self.auditStatusBtn.layer.cornerRadius = 8;
        self.auditStatusBtn.layer.borderWidth = 0;
        [self.auditStatusBtn setGradientBackgroundWithColors:@[kCOLOR_HEX(@"6AD785"),kCOLOR_HEX(@"5ECBB7")] orientation:GradientOrientationLeftToRight];
        
    }
    
    CGFloat btnWidth = [self.auditStatusBtn.titleLabel.text getStringWidthFont:self.auditStatusBtn.titleLabel.font limitHeight:25] + 20;
    [self.auditStatusBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_offset(btnWidth);
    }];
    
    CGFloat titleWidth = kUI_WINDOW_W - 30 - 30 - btnWidth - 15;
    CGFloat titleHeight = [self.titleLab.text getStringHeightFont:self.titleLab.font limitWidth:titleWidth];
    
    [self.titleLab mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.offset(titleHeight+1);
        make.width.offset(titleWidth);
    }];
    kWeakSelf;
    [self.videoTopContraint uninstall];
    UIView *refView = self.auditStatusBtn;
    if (titleHeight>20) {
        refView = self.titleLab;
    }
    [self.videoView mas_updateConstraints:^(MASConstraintMaker *make) {
        self.videoTopContraint = make.top.equalTo(refView.mas_bottom).offset(15);
    }];
    [self.videoView setNeedsLayout];
    [self.videoView layoutIfNeeded];
}

-(void)setupUI{
    kWeakSelf;
    self.contentView.backgroundColor = CFTabViewBgColor;
    UIView *bgView = kView;
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    [self.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(15, 15, 0, 15));
        
    }];
    
    _auditStatusBtn = [[QMUIButton alloc]init];
    _auditStatusBtn.titleLabel.font = kSYSTEMFONT(12);
    [_auditStatusBtn setTitleColor:kCOLOR_HEX_A(@"#3F444366", 0.4) forState:UIControlStateNormal];
    [_auditStatusBtn setTitle:@"--" forState:UIControlStateNormal];
    [bgView addSubview:_auditStatusBtn];
    [_auditStatusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-15);
        make.height.mas_offset(33);
        make.top.mas_offset(15);
        make.width.offset(60);
    }];
    
    QMUILabel *titleLab = [[QMUILabel alloc]init];
    titleLab.text = @"--";
    titleLab.font = [UIFont systemFontOfSize:15];
    titleLab.textColor = kCOLOR_HEX(@"3F4443");
    titleLab.numberOfLines = 0;
    [bgView addSubview:titleLab];
    self.titleLab = titleLab;
    
    UIView *videoView = kView;
    videoView.backgroundColor = [UIColor blackColor];
    videoView.layer.cornerRadius = 10;
    videoView.layer.masksToBounds = YES;
    [bgView addSubview:videoView];
    self.videoView = videoView;
    
    //
    _videoCoverView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"server_video_placeholder"]];
    _videoCoverView.contentMode = UIViewContentModeScaleAspectFill;
    _videoCoverView.sd_imageTransition = [SDWebImageTransition fadeTransition];
    _videoCoverView.userInteractionEnabled = YES;
    _videoCoverView.layer.cornerRadius = 8;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
    [_videoCoverView addGestureRecognizer:tap];
    [videoView addSubview:_videoCoverView];
    
    //播放按钮
    UIImageView *playIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"service_video_play"]];
    [_videoCoverView addSubview:playIcon];
    [playIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(40);
        make.center.mas_offset(0);
    }];
    
    
    //被拒蒙版本
    self.effectView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect qmui_effectWithBlurRadius:2]];
    [videoView addSubview:self.effectView];
    [self.effectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];
    self.effectView.effect = [UIBlurEffect qmui_effectWithBlurRadius:5];
    //被拒
    _remarkLab = [QMUILabel labelWithFont:kSYSTEMFONT(12) textColor:UIColor.whiteColor textAligment:NSTextAlignmentCenter];
    _remarkLab.numberOfLines = 0;
    _remarkLab.backgroundColor = kCOLOR_HEX_A(@"000000", 0.2);
    _remarkLab.contentEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
    [videoView addSubview:_remarkLab];
    [_remarkLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];
    _plantNameLab = [QMUILabel labelWithFont:kSYSTEMFONT(12) textColor:kCOLOR_HEX(@"3F4443") textAligment:NSTextAlignmentLeft];
    _plantNameLab.numberOfLines = 2;
    [bgView addSubview:_plantNameLab];
    
    
    _deleteBtn = [[QMUIButton alloc]init];
    _deleteBtn.titleLabel.font = kSYSTEMFONT(12);
    [_deleteBtn setTitleColor:kCOLOR_HEX_A(@"#3F4443", 0.6) forState:UIControlStateNormal];
    [_deleteBtn setTitle:@"删除".rh_localized forState:UIControlStateNormal];
    [_deleteBtn setImage:[UIImage imageNamed:@"delete_icon"] forState:UIControlStateNormal];
    [bgView addSubview:_deleteBtn];
    CGFloat btnWidth = [_deleteBtn.titleLabel.text getStringWidthFont:_deleteBtn.titleLabel.font limitHeight:20];
    [_deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-15);
        make.height.mas_offset(35);
        make.top.equalTo(videoView.mas_bottom).offset(5);
        make.width.offset(btnWidth + 40);
        make.bottom.mas_offset(-10);
    }];
    
    [_plantNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(videoView);
        make.centerY.equalTo(weakSelf.deleteBtn);
        make.right.equalTo(weakSelf.deleteBtn.mas_left).offset(-5);
    }];
    
    
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.mas_offset(20);
    }];
    
    
    [videoView mas_makeConstraints:^(MASConstraintMaker *make) {
      weakSelf.videoTopContraint =  make.top.equalTo(weakSelf.titleLab.mas_bottom).offset(15);
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
    }];
    
    [_videoCoverView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_offset(0);
        make.height.equalTo(weakSelf.videoCoverView.mas_width).multipliedBy(9/16.0);
        make.bottom.mas_offset(0);
    }];
    
    [[_deleteBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        if(weakSelf.deleteAction){
            weakSelf.deleteAction(weakSelf.videoModel);
        }
    }];
}

- (void)handleTapGesture:(id)sender {
    if ( [(id)_delegate respondsToSelector:@selector(coverItemWasTapped:)] ) {
        [_delegate coverItemWasTapped:self];
    }
}
@end
